# -*- coding: iso-8859-1 -*-
# file RigidBody.py
# author Rocco Martino
#
############################################################################
#    Copyright (C) 2015 by Rocco Martino                                   #
#    martinorocco@gmail.com                                                #
#                                                                          #
#    This program is free software; you can redistribute it and/or modify  #
#    it under the terms of the GNU Lesser General Public License as        #
#    published by the Free Software Foundation; either version 2.1 of the  #
#    License, or (at your option) any later version.                       #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU Lesser General Public License for more details.                   #
#                                                                          #
#    You should have received a copy of the GNU Lesser General Public      #
#    License along with this program; if not, write to the                 #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

############################################################################
from . import Transformable
############################################################################




############################################################################
# ........................................................................ #
# ........................................................................ #
############################################################################




############################################################################
# ........................................................................ #
class RigidBody(Transformable.Transformable):





############################################################################
############################################################################









############################################################################
    def __init__(self, obj, data):
        super(RigidBody, self).__init__(obj, data)

        self.ClassName = "osgODE::RigidBody"
############################################################################




############################################################################
    def writeData(self, writer):
        super(RigidBody, self).writeData(writer)

        position, quaternion, scale = self.BlenderObject.matrix_local.decompose()

        writer.writeLine("Position %f %f %f" %(position.x, position.y, position.z))
        writer.writeLine("Quaternion %f %f %f %f" %(quaternion[1], quaternion[2], quaternion[3], quaternion[0]))





        try:
            gravity_mode = self.BlenderObject["oo_gravity_mode"] > 0.5
        except:
            gravity_mode = True



        if not gravity_mode:
            writer.writeLine( "GravityMode FALSE" )




        if not self.BlenderObject.game.use_sleep:
            writer.writeLine("AutoDisableFlag TRUE")
        else:
            writer.writeLine("AutoDisableFlag FALSE")


        auto_disable_linear_threshold = self.Data.CurrentScene.game_settings.deactivation_linear_threshold
        auto_disable_angular_threshold = self.Data.CurrentScene.game_settings.deactivation_angular_threshold
        auto_disable_time = self.Data.CurrentScene.game_settings.deactivation_time
        auto_disable_steps = self.Data.CurrentScene.game_settings.deactivation_time * self.Data.CurrentScene.game_settings.fps
        auto_disable_average_samples_count = auto_disable_steps / 10


        writer.writeLine("AutoDisableLinearThreshold %f" %auto_disable_linear_threshold)
        writer.writeLine("AutoDisableAngularThreshold %f" %auto_disable_angular_threshold)
        writer.writeLine("AutoDisableSteps %u" %auto_disable_steps)
        writer.writeLine("AutoDisableTime %f" %auto_disable_time)
        writer.writeLine("AutoDisableAverageSamplesCount %i" %auto_disable_average_samples_count)



        writer.writeLine("LinearDamping %f" %self.BlenderObject.game.damping)
        writer.writeLine("AngularDamping %f" %self.BlenderObject.game.rotation_damping)
        writer.writeLine("LinearDampingThreshold %f" %self.BlenderObject.game.velocity_min)
        writer.writeLine("AngularDampingThreshold %f" %self.BlenderObject.game.velocity_min)



        if self.BlenderObject.game.velocity_max > 0.0:
            writer.writeLine("MaxAngularSpeed %f" %self.BlenderObject.game.velocity_max)



        writer.writeLine("Mass %f" %self.BlenderObject.game.mass)



        if self.BlenderObject.game.physics_type == 'STATIC':
            writer.writeLine("Kinematic TRUE")
############################################################################




# ........................................................................ #
############################################################################
